// pages/mobx/mobx.js
import {ComponentWithStore} from "mobx-miniprogram-bindings"
import {numStore} from "@/store/numstore.js"
import Dialog from "@vant/weapp/dialog/dialog"
import { getBorrowerDept } from "@/api/my.js"
const app = getApp()
ComponentWithStore({
  storeBindings:{
    store: numStore,
    fields:["num1","num2","sum"],
    actions:["update"] 
  },
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    username:"",
    userId:"",
    deptname:""
  },
  methods:{
    getDeptName() {
      getBorrowerDept(this.data.userId).then(res => {
        console.log(res.data.departments,"部门");
        if(res.data.departments.length > 0){
          this.setData({
            deptname: res.data.departments[0].deptName
          });
        }
      });
    },
    makePhone(){
      wx.makePhoneCall({
        phoneNumber: '400-123-456',
      })
    },
    logout(){
      Dialog.confirm({
        title: '提示',
        message: '确定要退出登录吗？',
      }).then(() => {
        // 退出登录
        wx.removeStorageSync("username")
        // 跳转到登录页面
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }).catch(() => {
        // 取消操作
      })
    },
  },
  lifetimes:{
    attached(){
      this.setData({
        username:wx.getStorageSync('username'),
        userId:wx.getStorageSync('id')
      })
      this.getDeptName()
    }
  }
})